modifier_custom_attributes = class({})

local public = modifier_custom_attributes

local attrs = {
	'health',
	'health_regen',
	'strength',
	'agility',
	'intellect',
	'mana',
	'armor',
	'mana_regen',
	'attack_speed',
	'move_speed',
	'damage_outgoing',
	'incoming_damage',
	'health_regen_percentage',
	'magic_armor',
}
for i,v in ipairs(attrs) do
	LinkLuaModifier("modifier_custom_attribute_"..v, "modifiers/attributes/"..v,LUA_MODIFIER_MOTION_NONE)
end

-- local m_modifier_funcs=
-- {
-- 	MODIFIER_PROPERTY_HEALTH_BONUS,
-- 	MODIFIER_PROPERTY_HEALTH_REGEN_CONSTANT,
-- 	MODIFIER_PROPERTY_HEALTH_REGEN_PERCENTAGE,
-- 	MODIFIER_PROPERTY_STATS_STRENGTH_BONUS,
-- 	MODIFIER_PROPERTY_STATS_AGILITY_BONUS,
-- 	MODIFIER_PROPERTY_STATS_INTELLECT_BONUS,
-- 	MODIFIER_PROPERTY_MANA_BONUS,
-- 	MODIFIER_PROPERTY_PHYSICAL_ARMOR_BONUS,
-- 	-- MODIFIER_PROPERTY_MAGICAL_RESISTANCE_BONUS,
-- 	MODIFIER_PROPERTY_MANA_REGEN_CONSTANT,
-- 	MODIFIER_PROPERTY_ATTACKSPEED_BONUS_CONSTANT,
-- 	--MODIFIER_PROPERTY_MOVESPEED_BONUS_CONSTANT,
-- 	-- MODIFIER_PROPERTY_MOVESPEED_MAX,
-- 	MODIFIER_PROPERTY_MOVESPEED_ABSOLUTE_MAX,
-- 	MODIFIER_PROPERTY_MOVESPEED_ABSOLUTE,
-- 	MODIFIER_PROPERTY_TOTALDAMAGEOUTGOING_PERCENTAGE,
-- 	MODIFIER_PROPERTY_INCOMING_DAMAGE_PERCENTAGE,
-- }

--------------------------------------------------------------------------------

function public:IsDebuff()
	return false
end

--------------------------------------------------------------------------------

function public:IsHidden()
	return true
end

--------------------------------------------------------------------------------

function public:IsPurgable()
	return false
end

--------------------------------------------------------------------------------

function public:RemoveOnDeath()
	return false
end

--------------------------------------------------------------------------------

function public:OnCreated(keys)
	self:StartIntervalThink(0.2)
end

--------------------------------------------------------------------------------

function public:OnIntervalThink()
	if IsServer() then
		local parent = self:GetParent()
		if parent:IsIllusion() and not parent:IsAlive() then return self:Destroy() end

		for k,v in pairs(parent:GetAllCustomAttribute()) do
			if k == 'hp' then
				self:SetCustomAttribute( 'health', v )
			elseif k == 'health_regen' then
				self:SetCustomAttribute( 'health_regen', v*100 )
			elseif k == 'str' then
				self:SetCustomAttribute( 'strength', v )
			elseif k == 'agi' then
				self:SetCustomAttribute( 'agility', v )
			elseif k == 'int' then
				self:SetCustomAttribute( 'intellect', v )
			elseif k == 'mana' then
				self:SetCustomAttribute( 'mana', v )
			elseif k == 'armor' then
				-- self:SetCustomAttribute( 'armor', v )
			elseif k == 'mana_regen' then
				self:SetCustomAttribute( 'mana_regen', v*100 )
			elseif k == 'attack_speed' then
				self:SetCustomAttribute( 'attack_speed', v )
			elseif k == 'move_speed' then
				v = v + parent:GetAgility() * 0.3
				self:SetCustomAttribute( 'move_speed', v )
			elseif k == 'damage_outgoing' then
				self:SetCustomAttribute( 'damage_outgoing', v )
			elseif k == 'incoming_damage' then
				self:SetCustomAttribute( 'incoming_damage', v )
			elseif k == 'health_regen_pct' then
				self:SetCustomAttribute( 'health_regen_percentage', v )
			elseif k == 'magic_armor' then
				self:FixedMagicArmor(v)
			end
		end
	end
end

function public:SetCustomAttribute( name, count )
	local parent = self:GetParent()
	local modifierName = 'modifier_custom_attribute_'..name
	local modifier = parent:FindModifierByName(modifierName)
	if modifier then
		modifier:SetStackCount(count)
	else
		local modifier = parent:AddNewModifier( parent, nil, modifierName, nil )
		if modifier then
			modifier:SetStackCount(count)
		end
	end
end

function public:GetCustomMoveSpeed( baseSpeed )
	local caster = self:GetCaster()
	local moveSpeed = 0
	local moveSpeedPercent = 0
	for i=0,5 do
		local item = caster:GetItemInSlot( i )
		if item then
			local speed = item:GetSpecialValueFor( "bonus_movement_speed" )
			if speed > moveSpeed then
				moveSpeed = speed
			end
			local speedPercent = item:GetSpecialValueFor( "bonus_movement_speed_pct" )
			if speedPercent > moveSpeedPercent then
				moveSpeedPercent = speedPercent
			end
		end
	end

	if caster:HasModifier("modifier_ability_shushan_evade") then
		moveSpeedPercent = moveSpeedPercent + 50
	end

	moveSpeedPercent = 1 + moveSpeedPercent / 100
	moveSpeedPercent = moveSpeedPercent + caster:GetAgility() * 0.0008
	return (400 + moveSpeed + baseSpeed) * moveSpeedPercent
end

function public:FixedMagicArmor( value )
	-- local magicArmor = 0
	-- local caster = self:GetCaster()
	-- caster:SetBaseMagicalResistanceValue((1 -  (1 - (magicArmor) )/ (1-caster:GetStrength()*0.0008))*100)
	self:SetCustomAttribute( 'magic_armor', value )
end

-- --------------------------------------------------------------------------------

function public:DeclareFunctions()
	return {MODIFIER_PROPERTY_IGNORE_MOVESPEED_LIMIT}
end

function public:GetModifierIgnoreMovespeedLimit()
	return 1
end

-- --------------------------------------------------------------------------------

-- -- 生命值加成
-- function public:GetModifierHealthBonus()
-- 	return self:GetAttrValue("hp")
-- end

-- --------------------------------------------------------------------------------

-- -- 生命回复速度
-- function public:GetModifierConstantHealthRegen()
-- 	return self:GetAttrValue("health_regen")
-- end

-- --------------------------------------------------------------------------------

-- -- 力量
-- function public:GetModifierBonusStats_Strength()
-- 	return self:GetAttrValue("str")
-- end

-- --------------------------------------------------------------------------------

-- -- 敏捷
-- function public:GetModifierBonusStats_Agility()
-- 	return self:GetAttrValue("agi")
-- end

-- --------------------------------------------------------------------------------

-- -- 智力
-- function public:GetModifierBonusStats_Intellect()
-- 	return self:GetAttrValue("int")
-- end

-- --------------------------------------------------------------------------------

-- -- 魔法加成
-- function public:GetModifierManaBonus()
-- 	return self:GetAttrValue("mana")
-- end

-- --------------------------------------------------------------------------------

-- -- 护甲加成
-- function public:GetModifierPhysicalArmorBonus()
-- 	return self:GetAttrValue("armor")
-- end

-- --------------------------------------------------------------------------------

-- -- 魔抗加成
-- function public:GetModifierMagicalResistanceBonus()
-- 	return self:GetAttrValue("magic_armor")
-- end

-- --------------------------------------------------------------------------------

-- -- 魔法回复速度
-- function public:GetModifierConstantManaRegen()
-- 	return self:GetAttrValue("mana_regen")
-- end

-- --------------------------------------------------------------------------------

-- -- 攻击速度加成
-- function public:GetModifierAttackSpeedBonus_Constant()
-- 	return self:GetAttrValue("attack_speed")
-- end

-- --------------------------------------------------------------------------------

-- -- 移速加成
-- function public:GetModifierMoveSpeedBonus_Constant()
-- 	return self:GetAttrValue("move_speed")
-- end

-- --------------------------------------------------------------------------------

-- -- 最大移动速度
-- function public:GetModifierMoveSpeed_AbsoluteMax()
-- 	return 10000
-- end

-- function public:GetModifierMoveSpeed_Absolute()
-- 	if IsClient() then
-- 		return 0
-- 	end
-- 	local caster = self:GetCaster()
-- 	local moveSpeed = 0
-- 	local moveSpeedPercent = 0
-- 	for i=0,5 do
-- 		local item = caster:GetItemInSlot( i )
-- 		if item then
-- 			local speed = item:GetSpecialValueFor( "bonus_movement_speed" )
-- 			if speed > moveSpeed then
-- 				moveSpeed = speed
-- 			end
-- 			local speedPercent = item:GetSpecialValueFor( "bonus_movement_speed_pct" )
-- 			if speedPercent > moveSpeedPercent then
-- 				moveSpeedPercent = speedPercent
-- 			end
-- 		end
-- 	end

-- 	if caster:HasModifier("modifier_ability_shushan_evade") then
-- 		moveSpeedPercent = moveSpeedPercent + 50
-- 	end

-- 	moveSpeedPercent = 1 + moveSpeedPercent / 100
-- 	moveSpeedPercent = moveSpeedPercent + caster:GetAgility() * 0.0008
-- 	return (400 + moveSpeed + self:GetAttrValue("move_speed")) * moveSpeedPercent
-- end

-- --------------------------------------------------------------------------------

-- -- 伤害输出增强
-- function public:GetModifierTotalDamageOutgoing_Percentage()
-- 	return self:GetAttrValue("damage_outgoing")
-- end

-- --------------------------------------------------------------------------------

-- -- 伤害吸收
-- function public:GetModifierIncomingDamage_Percentage()
-- 	return self:GetAttrValue("incoming_damage")
-- end

-- --------------------------------------------------------------------------------

-- -- 生命恢复速度%
-- function public:GetModifierHealthRegenPercentage()
-- 	return self:GetAttrValue("health_regen_pct")
-- end

-- --------------------------------------------------------------------------------